<template>
	<view class="">
		<view v-if="status > 0" class="unenable-switch">
			<view class="switch-circle"></view>
		</view>
		<view v-else class="disabled-switch">
			<view class="switch-circle"></view>
		</view>
	</view>
</template>

<script>
	export default {
		name: 'EvanSwitch',
		props: {
			status: {
				type: Number,
				default: 0
			},
			value: {
				type: [String, Number, Boolean],
				default: false
			},
			activeColor: {
				type: String,
				default: '#108ee9'
			},
			inactiveColor: {
				type: String,
				default: '#fff'
			},
			size: {
				type: Number,
				default: 30
			},
			width: Number,
			height: Number,
			disabled: {
				type: Boolean,
				default: false
			},
			activeValue: {
				type: [String, Number, Boolean],
				default: true
			},
			inactiveValue: {
				type: [String, Number, Boolean],
				default: false
			},
			beforeChange: {
				type: Function,
				default: null
			}
		},
		computed: {
			switchHeight() {
				// #ifdef APP-NVUE
				return this.size + 2 + 'px'
				// #endif
				// #ifndef APP-NVUE
				return this.size + 'px'
				// #endif
			}
		},
		watch: {
			value: {
				immediate: true,
				handler(value) {
					this.currentValue = value
				}
			}
		},
		data() {
			return {
				currentValue: false
			}
		},
		methods: {
			toggle() {
				if (!this.disabled) {
					if (this.beforeChange && typeof this.beforeChange === 'function') {
						const result = this.beforeChange(this.currentValue === this.activeValue ? this.inactiveValue : this.activeValue)
						if (typeof result === 'object') {
							result.then(() => {
								this.toggleValue()
							}).catch(() => {})
						} else if (typeof result === 'boolean' && result) {
							this.toggleValue()
						}
					} else {
						this.toggleValue()
					}
				}
			},
			toggleValue() {
				this.currentValue = this.currentValue === this.activeValue ? this.inactiveValue : this.activeValue
				this.$emit('input', this.currentValue)
				this.$emit('change', this.currentValue)
			}
		}
	}
</script>

<style lang="scss" scoped>
	
	// 不可用
	.disabled-switch {
		display: flex;
		align-items: center;
		justify-content: flex-end;
		position: relative;
		width: 76rpx;
		height: 40rpx;
		border-radius:20rpx;
		background:rgba(238,238,238,1);
		transition: background-color 0.3s;
		
		.switch-circle {
			margin-left: 1px;
			margin-right: auto;
			width: 34rpx;
			height: 34rpx;
			border-radius:50%;
			box-shadow:0px 0px 8rpx 0px rgba(0, 0, 0, 0.1);
			background:rgba(255,255,255,1);
			transition: transform 0.3s;
		}
	}
	
	
	// 可用
	.unenable-switch {
		display: flex;
		align-items: center;
		position: relative;
		width: 76rpx;
		height: 40rpx;
		border-radius:20rpx;
		background:rgba(224,19,39,1);
		transition: background-color 0.3s;
		
		.switch-circle {
			margin-left: auto;
			margin-right: 1px;
			width: 34rpx;
			height: 34rpx;
			border-radius:50%;
			box-shadow:0px 0px 8rpx 0px rgba(0, 0, 0, 0.1);
			background:rgba(255,255,255,1);
			transition: transform 0.3s;
		}
	}
</style>




